﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QQ.Util;
using AccessDb;

namespace SoftwareManager
{
    public partial class AddForm : Form
    {

        /// <summary>
        /// 定义更目录
        /// </summary>
        private static string rootpath = Application.StartupPath;//根目录
        /// <summary>
        /// 定义数据库的位置
        /// </summary>
          private string dbpath = rootpath + "\\DB.mdb";
        private string userlist = rootpath + "\\人员名单.txt";
        private string softwareList = rootpath + "\\软件名单.txt";

        private string id="";

        public event Action<bool> ReturnHandler;


        /// <summary>
        /// 设置ID
        /// </summary>
        public string ID
        {
            set
            {
                id = value;
            }
        }



        public AddForm()
        {
            InitializeComponent();
        } 
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (safeCheck() == true)
            {
                if (id.Trim() != "")//修改数据
                {
                    bool succ = updataMachine();
                    if (succ == true)
                    {
                        MessageBox.Show("更新機器碼成功");
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("更新機器碼失败！");
                    }

                }
                else//增加数据
                {
                   bool succ= addMachine();
                   if (succ == true)
                   {
                       MessageBox.Show("新增機器碼成功");
                       this.Dispose();
                   }
                   else
                   {
                       MessageBox.Show("新增機器碼失败！");
                   }
                }
            }
            else
            {
                MessageBox.Show("數據填寫不完整！請檢查一下哦！");
                return;
            } 
        }



        /// <summary>
        /// 增加机器码
        /// </summary>
        /// <returns></returns>
        private bool addMachine()
        {
            string machinecode = txt_machinecode.Text;
            string givecode = txt_givecode.Text;
            string email = txt_email.Text;
            string phone = txt_phone.Text;
            string username = cb_username.Text;
            string softname = cb_softtype.Text;
            string pid = txt_pid.Text;
            string remark = txt_remark.Text;
            string key = txt_key.Text; 
            string sql = string.Format("insert into machine (machinecode,givecode,username,softname,pid,mkey,phone,email,remark) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", 
                machinecode,givecode,username,softname,pid,key,phone,email,remark);


            
            AccessDbClass au = new AccessDbClass(dbpath);
            bool succ=au.ExecuteSQLNonquery(sql);
            au.Close(); 
            ///回调函数
            if (this.ReturnHandler != null)
            {
                this.ReturnHandler(succ);
            }
            return succ; 
        }

        /// <summary>
        /// 更新机器码
        /// </summary>
        /// <returns></returns>
        private bool updataMachine()
        {
            string machinecode = txt_machinecode.Text;
            string givecode = txt_givecode.Text;
            string email = txt_email.Text;
            string phone = txt_phone.Text;
            string username = cb_username.Text;
            string softname = cb_softtype.Text;
            string pid = txt_pid.Text;
            string remark = txt_remark.Text;
            string key = txt_key.Text;
            string sql = string.Format("update machine set machinecode='{0}',givecode='{1}',username='{2}',softname='{3}',pid='{4}',mkey='{5}',phone='{6}',email='{7}',remark='{8}' where id={9}",
                machinecode, givecode, username, softname, pid, key, phone, email, remark,this.id);

            AccessDbClass au = new AccessDbClass(dbpath);
            bool succ = au.ExecuteSQLNonquery(sql);
            au.Close();
            ///回调函数
            if (this.ReturnHandler != null)
            {
                this.ReturnHandler(succ);
            }
            return succ; 
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {

            this.Dispose();
        }

        /// <summary>
        /// 安全检查函数
        /// </summary>
        /// <returns></returns>
        private bool safeCheck()
        {
            if (txt_machinecode.Text.Trim() != "" 
               // && txt_givecode.Text.Trim() != ""
               /// && txt_email.Text.Trim() != "" 
                && cb_softtype.Text.Trim() != ""
                && cb_username.Text.Trim() != ""
               // && txt_phone.Text.Trim() != ""                
               // && txt_pid.Text.Trim() != ""
               // && txt_key.Text.Trim() != ""
                
                )
            { 
                return true; 
            }
            else
            {
                return false; 
            } 
        }
        /// <summary>
        /// 窗口初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddForm_Load(object sender, EventArgs e)
        {
            //读取姓名和软件名称
           List<string> user= TextBoxTools.getFileList(userlist);
           List<string> soft = TextBoxTools.getFileList(softwareList); 
           foreach (string tem in user)
           {
               cb_username.Items.Add(tem);
           }
           foreach (string str in soft)
           {
               cb_softtype.Items.Add(str);
           } 
            //==============判断一下是添加还是修改=============

           if (id != "")//说明是修改内容的
           {

               string sql = string.Format("select * from machine where id={0}",id);
               AccessDbClass au = new AccessDbClass(dbpath);
               DataTable dt = au.SelectToDataSet(sql, "machine").Tables[0];
               au.Close();
               if (dt.Rows.Count != 0)
               {
                   string machinecode = dt.Rows[0]["machinecode"].ToString();
                   string username = dt.Rows[0]["username"].ToString();
                   string softname = dt.Rows[0]["softname"].ToString();
                   string key = dt.Rows[0]["mkey"].ToString();
                   string pid = dt.Rows[0]["pid"].ToString();
                   string phone = dt.Rows[0]["phone"].ToString();
                   string email = dt.Rows[0]["email"].ToString();
                   string remark = dt.Rows[0]["remark"].ToString(); 
                   string givecode=dt.Rows[0]["givecode"].ToString(); 
                
                   //开始给对象进行赋值

                   txt_email.Text = email;
                   txt_givecode.Text = givecode;
                   txt_key.Text = key;
                   txt_machinecode.Text = machinecode;
                   txt_phone.Text = phone;
                   txt_pid.Text = pid;
                   txt_remark.Text = remark;
                   cb_softtype.Text = softname;
                   cb_username.Text = username;
               } 
             
           }
             
        }
    }
}
